package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.DevicIccRelativeMapper;
import com.showcai.module.ApplyWatch;
import com.showcai.module.DevicIccRelative;
import com.showcai.module.ExpressInfo;
import com.showcai.module.Order;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2018-9-27.
 */
@Service
public class DevicIccRelativeServiceImpl extends ServiceBase implements DevicIccRelativeService {
    @Autowired
    DevicIccRelativeMapper devicIccRelativeMapper;
    @Autowired
    ExpressInfoService expressInfoService;
    @Autowired
    ApplyService applyWatchService;
    @Autowired
    OrderService orderService;

    private void insert(DevicIccRelative devicIccRelative) {
        devicIccRelativeMapper.insertDevicIccRelative(devicIccRelative);
    }

    private void updata(DevicIccRelative devicIccRelative) {
        devicIccRelativeMapper.updateDevicIccRelativeByPrimaryKeySelective(devicIccRelative);
    }


    @Override
    public DevicIccRelative modifyDevicIccRelative(DevicIccRelative devicIccRelative) {

        if (StringUtils.isBlank(devicIccRelative.getIccId())) {
            throw new MyRunTimeExcption("iccId is null");
        }
        Map<String, String> map = new HashMap<>();
        if (StringUtils.isNotBlank(devicIccRelative.getDevicId())) {
            map.put("devicId", devicIccRelative.getDevicId());
        }
        map.put("iccId", devicIccRelative.getIccId());
        devicIccRelative.setCreateTime(new Date());
        synchronized (this.getClass()){
            DevicIccRelative devicIccRelative1 = devicIccRelativeMapper.selectDevicIccRelative(map);
            if (devicIccRelative1 != null) {
                devicIccRelative.setStatus(devicIccRelative1.getStatus());
                return devicIccRelative;
            }
            devicIccRelative.setStatus(DevicIccRelative.STATUS_VALID);
            insert(devicIccRelative);
        }
        map.put("info",devicIccRelative.getIccId());
        ExpressInfo expressInfo = expressInfoService.selectExpressInfo(map);
        if (expressInfo != null) {
            Long orderId = expressInfo.getOrderId();
            Order order = orderService.getOrderById(orderId);
            if (order.getStatus() == OrderService.DISPATCHED) {
                orderService.modifyOrderStatus(orderId, OrderService.RECEIVED);
                ApplyWatch applyWatch = (ApplyWatch) applyWatchService.selectApplyByOrderId(orderId);
                applyWatch.setStatus(OrderService.RECEIVED);
                applyWatchService.modifyApply(applyWatch);
            }
        }
        return devicIccRelative;
    }

    @Override
    public DevicIccRelative selectDevicIccRelative(Map<String, String> map) {
        return devicIccRelativeMapper.selectDevicIccRelative(map);
    }

    @Override
    public PageInfo selectDevicIccRelativePageInfoList(Map<String, String> map) {


        Integer page = Integer.parseInt(map.get("page"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(page, pageSize);
        List<DevicIccRelative> devicIccRelativeList = devicIccRelativeMapper.selectDevicIccRelativeList(map);
        PageInfo<DevicIccRelative> pageinfo = new PageInfo<>(devicIccRelativeList);
        return pageinfo;
    }

    @Override
    public DevicIccRelative modifyDevicIccRelativeStatus(Long tid, Byte status) {
        DevicIccRelative devicIccRelative = devicIccRelativeMapper.selectDevicIccRelativeById(tid);
        devicIccRelative.setStatus(status);
        updata(devicIccRelative);
        return devicIccRelative;
    }
}
